//
//  UserCompanyCell2.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/9/1.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
protocol UserCompanyCell2Delegate {
    func collectionLicenceSelected()
    func collectionStyleSelected()
    func photoBtnSelected(type: String)
    func getUserInfo()
    
}
class UserCompanyCell2: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, FactoryCollCellDelegate {
    var modelLis: [userPersonPhoto]?{
        didSet{
            if self.modelLis?.count > 0{
                self.photoBtn.hidden = true
                
            }else{
                deleteBtnFlag = true
                rotateAniFlag = true
                self.photoBtn.hidden = false
            }
            self.collection.reloadData()
            
        }
    }
    var type: String?//   type == "1"营业执照   type == "2"企业风采
    
    var delegate:UserCompanyCell2Delegate?
    @IBOutlet weak var collection: UICollectionView!
    @IBOutlet weak var nameLbl: UILabel!
    @IBOutlet weak var explainLbl: UILabel!
    @IBOutlet weak var photoBtn: UIButton!
    
    var model: userPersonCompany?{
        didSet{
            if type == "1" {
                self.modelLis = model?.unitlicense
                
            }else if type == "2" {
                self.modelLis = model?.unitstyle
            }
            
           
        }
    }
    
    var deleteBtnFlag:Bool!
    var rotateAniFlag:Bool!
   
    override func awakeFromNib() {
        super.awakeFromNib()
        self.collection.dataSource = self
        self.collection.delegate = self
        self.collection.scrollEnabled = false
        let cellNib = UINib(nibName: "FactoryCollCell", bundle: nil)
        self.collection.registerNib(cellNib, forCellWithReuseIdentifier: "FactoryCollCell")
        self.addDoubleTapGesture()
        deleteBtnFlag = true
        rotateAniFlag = true
    }
    
    func addDoubleTapGesture() {
        let doubletap = UITapGestureRecognizer(target: self, action: #selector(self.handleDoubleTap))
        doubletap.numberOfTapsRequired = 2
        self.collection!.addGestureRecognizer(doubletap)
    }
    
    func handleDoubleTap(gestureRecognizer: UITapGestureRecognizer) {
        self.hideAllDeleteBtn()
    }
    
    func hideAllDeleteBtn() {
        if !deleteBtnFlag {
            deleteBtnFlag = true
            rotateAniFlag = true
            self.collection!.reloadData()
        }
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    func setCellContentWithCellInfo(cellInfo: [userPersonPhoto]?, name: String, explain: String) -> Void {
        
        self.modelLis = cellInfo
        self.nameLbl.text = name
        self.explainLbl.text = explain
        
    }
  
    @IBAction func photoBtnClicked(sender: UIButton){
        
        delegate?.photoBtnSelected(self.type!)
    }


    //MARK:UICollectionViewDataSource
    //每一组有多少个cell
    func collectionView(collectionView:UICollectionView,numberOfItemsInSection section: Int) -> Int{
        
        if self.modelLis?.count != 0 && self.modelLis?.count != nil{
            return (self.modelLis?.count)!
        }else{
            return 0
        }
        
    }
    //设置每组的cell的边界
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,insetForSectionAtIndex section: Int) -> UIEdgeInsets{
        
        return UIEdgeInsetsMake(10, 10, 10, 10)//上左下右
    }
    //最小行间距
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat{
        return 10
    }
    //最小列间距
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat{
        return 10
    }
    //设定全局的Cell尺寸
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
        
        return CGSizeMake(SCREEN_WIDTH/3-20,80)
        
    }
    //cell上显示的信息
    func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) ->UICollectionViewCell{
        let cell = collection.dequeueReusableCellWithReuseIdentifier("FactoryCollCell", forIndexPath: indexPath) as! FactoryCollCell
        cell.indexPath = indexPath
        cell.delegate = self
        if deleteBtnFlag == true{
            cell.deleteBtn.hidden = true
        }else{
            cell.deleteBtn.hidden = false
        }
        if (!rotateAniFlag) {
            YTAnimation.vibrateAnimation(cell)
        }else{
            cell.layer.removeAnimationForKey("shake")
        }
        
        cell.iv.sd_setImageWithURL(NSURL(string: self.modelLis![indexPath.item].imgsrc), placeholderImage: UIImage(named: "placeholder"))
        
        return cell

    }
    //MARK:UICollectionViewDelegate
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
        if self.type == "1"{
            delegate?.collectionLicenceSelected()
        }
        if self.type == "2"{
           delegate?.collectionStyleSelected()
        }
    }
    
    func deleteCellAtIndexpath(indexPath: NSIndexPath, cellView: UIView){
        
        if self.type == "1"{
            self.deletePhotos("user/dodel/unitlicense/", id: self.modelLis![indexPath.row].id)
            
        }else if self.type == "2"{
            self.deletePhotos("user/dodel/unitstyle/", id: self.modelLis![indexPath.row].id)
        }
        
        
    }
    func showAllDeleteBtn(type: String){
        print("showAll")
        deleteBtnFlag = false
        rotateAniFlag = false
        self.collection!.reloadData()
        
    }
    //MARK:networking
    //删除照片
    func deletePhotos(urlStr: String, id: String){
        
        DataTool.deletePhoto(urlStr, id: id, completionHandler:({ (newModel) in
            guard newModel != nil else {
                return
            }
            
            SVProgressHUD.showSuccessWithStatus(newModel?.msg)
            self.delegate?.getUserInfo()
        }))
    }
    


    
}
